package gamer2;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.media.opengl.GL2;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;

public class CTexture {

	public Texture texture = null;
	private GL2 gl = null;
	public int textureID[] = new int[1];
	// private C2DField field;

	public int width;
	public float centerValue = 0;
	private FloatBuffer buf;

//	private C2DField quadField;

	public void Initialize(GL2 gl) {

	}

	public CTexture(String filename, GL2 gl) {
		
		try {
			InputStream stream = getClass().getResourceAsStream(
				filename);
			TextureData data = TextureIO.newTextureData(gl.getGLProfile(),
				stream, true, "png");
			texture = TextureIO.newTexture(data);
/*			texture.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_S,
					GL2.GL_REPEAT);
			texture.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_T,
					GL2.GL_REPEAT);*/
/*			texture.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
			texture.setTexParameteri(gl, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
			texture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_LINEAR);
			texture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER,GL2.GL_LINEAR);
*/
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error CTexture.Initialize: " + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	
	public CTexture(GL2 g) {
		gl = g;

		Generate();

	}

/*	public CTexture(GL2 g, C2DField qf, boolean hasA) {
		gl = g;
		quadField = qf;
		width = quadField.width;
		if (buf == null) {
			buf = FloatBuffer.allocate(width * width * 4);
		}

		quadField.ToFloatBuffer(buf, hasA);

		Generate();
		// centerValue = generateBuffer();
		if (!hasA)
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, width, width, 0,
					GL2.GL_RGB, GL2.GL_FLOAT, buf);
		else
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA32F, width, width, 0,
					GL2.GL_RGBA, GL2.GL_FLOAT, buf);

		texture = null;
	}

	public CTexture(GL2 g, QuadField qf, boolean hasA) {
		gl = g;
		width = qf.sizeVBO;
		if (buf == null) {
			buf = FloatBuffer.allocate(width * width * 4);
		}

//		qf.ToFloatBuffer(buf, 1f,1f,1f);
		
		Generate();
		// centerValue = generateBuffer();
		if (!hasA)
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, width, width, 0,
					GL2.GL_RGB, GL2.GL_FLOAT, buf);
		else
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA32F, width, width, 0,
					GL2.GL_RGBA, GL2.GL_FLOAT, buf);

		texture = null;
	}

	*/
	private void Generate() {
		// Create Linear Filtered Texture
		gl.glGenTextures(1, textureID, 0); // Create One Texture
		gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
		gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
				GL2.GL_LINEAR);
		gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
				GL2.GL_LINEAR);
		gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
				GL2.GL_CLAMP_TO_EDGE);
		gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
				GL2.GL_CLAMP_TO_EDGE);

			gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
				GL2.GL_LINEAR_MIPMAP_LINEAR);

			gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_GENERATE_MIPMAP,
				GL2.GL_TRUE);
		
	}
	
	public CTexture(GL2 g, int width, FloatBuffer pbuf, boolean hasA, boolean mipmap) {
		gl = g;
		
		// texture = TextureIO.newTexture(buf);
		Generate();

		if (!hasA)
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, width, width, 0,
					GL2.GL_RGB, GL2.GL_FLOAT, pbuf);
		else
			gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA32F, width, width, 0,
					GL2.GL_RGBA, GL2.GL_FLOAT, pbuf);

		texture = null;
	}

	
	public void Enable(GL2 gl) {
		gl.glEnable(GL2.GL_TEXTURE_2D);
		gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
	}

	public void IgnoreMipmap(GL2 m_gl) {
		  m_gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
		  GL2.GL_LINEAR);
	}
	
	public void Disable(GL2 gl) {
		gl.glDisable(GL2.GL_TEXTURE_2D);
	}

}
